package cn.jslancer.webx.dispatcher;

import cn.jslancer.webx.context.WebxContext;
import cn.jslancer.webx.request.WebxRequest;
import cn.jslancer.webx.response.WebxResponse;
import cn.jslancer.webx.server.Server;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.FullHttpRequest;

/**
 * @author zhuangchao
 * @date 2018-07-27 16:26
 */
@ChannelHandler.Sharable
public class Dispatcher extends SimpleChannelInboundHandler<FullHttpRequest> {


    private Server server;

    public Dispatcher(Server server) {
        this.server = server;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
//        HttpPostRequestDecoder requestDecoder = new HttpPostRequestDecoder(msg);

        WebxRequest request = new WebxRequest(msg);

        WebxResponse response = new WebxResponse(ctx);

        WebxContext context = new WebxContext(request, response);

        // TODO 拦截器
        server.requestHandle(context);
    }

}
